package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.ScmDistributionDTO;
import com.xinsoft.entity.dto.ScmDistributionParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.entity.vo.ScmDistributionListVo;
import com.xinsoft.entity.vo.ScmDistributionVo;
import com.xinsoft.mapper.ScmDistributionItemMapper;
import com.xinsoft.service.ScmDistributionItemService;
import com.xinsoft.service.ScmDistributionService;
import com.xinsoft.mapper.ScmDistributionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author ZYM
* @description 针对表【scm_distribution(配送)】的数据库操作Service实现
* @createDate 2023-07-24 17:21:20
*/
@Service
public class ScmDistributionServiceImpl extends ServiceImpl<ScmDistributionMapper, ScmDistribution>
    implements ScmDistributionService{

    @Resource
    private ScmDistributionItemMapper distributionItemMapper;

    @Autowired
    private ScmDistributionItemService distributionItemService;

    @Override
    public IPage<ScmDistributionListVo> findPageListByParam(ScmDistributionParam param) {
        Page<ScmDistributionListVo> page = new Page<>(param.getCurrentPage(),param.getPageSize());
        return baseMapper.findPageListByParam(page,param);
    }

    @Override
    public ScmDistributionVo detail(Integer id) {
        ScmDistributionVo distributionVo = baseMapper.getDistributionById(id);
        // 车辆配送明细
        distributionVo.setScmDistributionItemList(distributionItemMapper.listByDisId(id));
        return distributionVo;
    }

    @Override
    public void tempSave(ScmDistributionDTO distributionDTO) {
        ScmDistribution distribution = new ScmDistribution();
        BeanUtils.copyProperties(distributionDTO, distribution);
        distribution.setAuditStatus(Const.AUDIT_STATUS_WAIT);
        distribution.setDoSign(0);
        distribution.setDisNum(distributionDTO.getScmDistributionItemList().stream().map(ScmDistributionItem::getItemNum).reduce(BigDecimal.ZERO,BigDecimal::add));
        List<ScmDistributionItem> itemList = distributionDTO.getScmDistributionItemList();
        // 保存或新增
        if (Objects.isNull(distribution.getId())) {
            this.save(distribution);
        } else {
            distributionItemService.remove(Wrappers.lambdaQuery(ScmDistributionItem.class).eq(ScmDistributionItem::getDisId, distribution.getId()));
            this.updateById(distribution);
        }

        // 配送明细
        itemList.forEach(item -> {
            item.setDisId(distribution.getId());
            item.setSourceType(Const.ApproveType.XSCK);
        });
        distributionItemService.saveBatch(itemList);
    }

    @Override
    public void del(List<Integer> ids) {
        List<ScmDistribution> list = this.list(Wrappers.lambdaQuery(ScmDistribution.class)
                .eq(ScmDistribution::getAuditStatus, Const.AUDIT_STATUS_NORMAL)
                .in(ScmDistribution::getId, ids));
        if (list.size() > 0 ) {
            throw new BDException("操作失败，只能删除暂存的销售出库单");
        }
        distributionItemService.remove(Wrappers.lambdaQuery(ScmDistributionItem.class).in(ScmDistributionItem::getDisId, ids));
        this.removeByIds(ids);
    }

    @Override
    public void batchConfirmReceipt(List<Integer> ids) {
        List<ScmDistribution> distributionList = this.list(Wrappers.lambdaQuery(ScmDistribution.class)
                .eq(ScmDistribution::getDoSign, 1)
                .in(ScmDistribution::getId, ids));
        if(distributionList.size() > 0){
            throw new BDException("已签收，无需重复签收");
        }
        List<ScmDistribution> distributionList1 = this.list(Wrappers.lambdaQuery(ScmDistribution.class)
                .eq(ScmDistribution::getDoSign, 0)
                .in(ScmDistribution::getId, ids));
        distributionList1.forEach(dis -> dis.setDoSign(1));
        this.updateBatchById(distributionList1);
    }
}




